<!--
 * @Author: liyin 2179253556@qq.com
 * @Date: 2022-11-16 08:08:04
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2025-04-29 15:47:02
 * @FilePath: Home.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->

<template>
  <div class="home">
    <div>
      <router-view></router-view>
    </div>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from 'vuex'
import $util from '../util/util'
export default {
  name: 'Home',
  data() {
    return {
      computTime: null, // 时间计算计时器
      timeListStart: [],
      timeListEnd: [],
      ruleTimes: [],
      timeData: '',
      selectData: '',
      ruleIdsOne: [],
      ruleIdsTwo: [],
      ruleIds: []
    }
  },
  methods: {
    ...mapActions([
      'setRuleIds',
      'setRuleTimeRange'
    ]),
    // 处理定点跳转时间跳转不同的页面
    handelTimes() {
      // console.log(this.timeData.substring(11, 19))
      // 如果下拉框选择的日期跟当前服务器的日期时间不相等，就证明是查找历史数据，不算实时考勤状态
      if (this.selectData !== this.timeData.substring(0, 10)) {
        return;
      }
      let ruleIdsOne = []
      let ruleIdsTwo = []
      this.timeListStart.forEach(item => {
        if ($util.isInTimeRange(this.timeData.substring(11, 19), item)) {
          ruleIdsTwo = []
          let ruleId = $util.isInTimeRange(this.timeData.substring(11, 19), item)
          if (!ruleIdsOne.includes(ruleId)) {
            ruleIdsOne.push(ruleId)
          }
        }
      })

      this.timeListEnd.forEach(item => {
        if ($util.isInTimeRange(this.timeData.substring(11, 19), item)) {
          ruleIdsOne = []
          let ruleId = $util.isInTimeRange(this.timeData.substring(11, 19), item)
          if (!ruleIdsTwo.includes(ruleId)) {
            ruleIdsTwo.push(ruleId)
            this.setRuleTimeRange(item.split('/')[0])
          }
        }
      })
      // console.log(ruleIdsOne, '???????????????????', ruleIdsTwo)
      this.ruleIdsTwo = ruleIdsTwo
      this.setRuleIds(this.ruleIdsTwo)
      if (ruleIdsOne.length > 0 && this.ruleIds.length === 0) {
        // 判断是否在某个时间范围内 宿舍评分页面
        // 跳转页面
        ruleIdsTwo = []
        this.ruleIdsTwo = []
        this.setRuleIds(this.ruleIdsTwo)
        this.setRuleTimeRange('')
        if (this.$route.path === '/dormitRate') {
          return
        }
        this.$router.push('/dormitRate');
      } else if (ruleIdsTwo.length > 0 && this.ruleIds.length > 0) {
        ruleIdsOne = []
        this.ruleIdsOne = []
        if (this.$route.path === '/dormitAttend') {
          return
        }
        // 跳转页面
        this.$router.push('/dormitAttend');
      } else {
        ruleIdsOne = []
        ruleIdsTwo = []
        this.ruleIdsOne = []
        this.ruleIdsTwo = []
        this.setRuleIds(ruleIdsTwo)
        this.setRuleTimeRange('')
        // 如果前面的都不走的话 就直接跳转底图 并且带上最开始的路径参数
        if (this.$route.path === '/homeView' || this.$route.path === '/') {
          return
        }
        this.$router.push({ path: '/homeView', query: Object.keys(JSON.parse(window.localStorage.getItem('routerParams'))).length > 0 ? JSON.parse(window.localStorage.getItem('routerParams')) : this.$route.query })
      }
    },
  },
  mounted() {
    this.ruleTimes = JSON.parse(window.localStorage.getItem('ruleTimes'))
    this.timeData = window.localStorage.getItem('timeData')
    this.selectData = window.localStorage.getItem('selectData')
    this.ruleIds = JSON.parse(window.localStorage.getItem('ruleIds'))
    //监听localStorage变化
    window.addEventListener("setItemEvent", (e) => {
      // 监听需要的键名
      if (e.key === 'timeData') {
        if (this.ruleTimes && this.ruleTimes.length > 0) {
          this.timeData = e.newValue
          this.handelTimes()
        }
      } else if (e.key === 'ruleTimes') {
        if (JSON.parse(e.newValue) && JSON.parse(e.newValue).length > 0) {
          this.timeListStart = []
          this.timeListEnd = []
          JSON.parse(e.newValue).forEach(item => {
            this.timeListStart.push(`${item.start_time}-${item.end_time}/${item.rule_id}/${item.add_time}/${item.in_out}/${item.rule_name}`)
            this.timeListEnd.push(`${item.end_time}-${$util.addMinutes(item.end_time, 30)}/${item.rule_id}/${item.add_time}/${item.in_out}/${item.rule_name}`)
          })
        }
        // console.log(this.timeListStart, '........', this.timeListEnd)
      } else if (e.key === 'ruleIds') {
        if (JSON.parse(e.newValue).length > 0 && e.newValue !== JSON.stringify(this.ruleIds)) {
          this.ruleIds = JSON.parse(e.newValue)
        }
      }
    })
  },
  created() {
  }
}
</script>
<style lang="less" scoped>
.home {
  width: 100%;
  height: 100%;
  position: relative;
  box-sizing: border-box;
}
</style>
